﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace app.Api
{
    /*
        公用接口
    */
    public class ApiFeedback : ApiBase
    {
        public ApiFeedback(Controller ctx) : base(ctx) { }


        //反馈
        public void Do(string action)
        {
            switch (action)
            {
                case "send":
                    {
                        string validate_code = ctx.Session["validate_code"].ToString();
                        Console.WriteLine("session中的validate_code" + validate_code);
                        string code = GetStringParam(new ReqStringParamOption { Name = "validate_code" }).ToUpper();
                        Console.WriteLine("从前台获得的validate_code"+ code);
                        string phone = GetStringParam(new ReqStringParamOption { Name = "phone", Level = REQPARAMLEVEL.FREE });
                        string email = GetStringParam(new ReqStringParamOption { Name = "email", Level = REQPARAMLEVEL.FREE });
                        string content = GetStringParam(new ReqStringParamOption { Name = "content" });
             
                        if (validate_code == code)
                        {
                            Feedback fb = new Feedback { name = req_name, phone = phone, email = email, content = content, create_time = DateTimeEx.GetNowTimeStamp(), member_id = UserCode };
                            G.DB.Insert<Feedback>(fb);
                            ResponseJson(new BooleanResult { value = true });
                           // ResponseJson(new Result());                           
                        }
                        else
                        {
                           // RecordLog(PERMISSIONOPERATION.LOGIN, "提交失败！原因：验证码错误。");
                            ResponseJson(new BooleanResult { value = false, message = "验证码错误" });
                        }
                        break;
                    }
                    //标记为已处理
                case "watched":
                    {
                        Feedback fb = G.DB.GetById<Feedback>(req_id);
                        fb.status = (int)FEEDBACK_SATTUS.PROCESSED;
                        fb.processor = UserName;
                        fb.disposs_time = DateTimeEx.GetNowTimeStamp();
                        G.DB.Update<Feedback>(fb);
                        if (fb.member_id != "")
                        {
                            SendMessage(Int32.Parse(fb.member_id), string.Format("反馈已受理", fb.create_time), string.Format("尊敬的用户【{0}】：工作人员已成功受理了您于【{1}】提交的反馈，非常感谢您提出的宝贵意见。",fb.name, DateTimeEx.TimeStampToDatetime(fb.create_time)));
                            ResponseJson(new ValueResult { value = fb.status });                          
                        }
                        else
                        {
                            ResponseJson(new ValueResult { value = fb.status });
                        }
                        break;
                    }
                case "search":
                    {
                        Cd cd = new Cd();
                        if (req_status != 0) cd.And("status={0}", req_status);
                        Ob ob = new Ob();
                        ob.Desc("id");
                        ResponseJson(GetPageResult<Feedback>(cd.Render(), ob.Render()));
                        break;
                    }
                case "delete":
                    {
                        //G.DB.Delete<Feedback>(req_id);
                        if (G.DB.Delete<Feedback>(req_id) == 0) ThrowStatus(STATUS.FAILED_DELETE, "*反馈删除失败！");
                        else ResponseJson(new Result());
                        break;
                    }
                case "delate_log":
                    {
                        if (G.DB.Delete<Log>(req_id) == 0) ThrowStatus(STATUS.FAILED_DELETE, "*日志删除失败！");
                        else ResponseJson(new Result());
                        break;
                    }
                default: break;
            }
        }
    }
}